<?php

namespace App\Http\Middleware;

use App\Models\Admin\Admin as AdminModel;
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AdminAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $adminid = $request->input('adminid','');

        $admin = AdminModel::find($adminid);

        if(!$admin)
        {
            return new JsonResponse(['code' => 0,'msg' => '该管理员不存在','data' => null]);
        }

        if($admin['status'] !== 'normal')
        {
            return new JsonResponse(['code' => 0,'msg' => '该管理员已被禁用','data' => null]);
        }

        $request->attributes->add(['admin' => $admin]);
        
        return $next($request);
    }
}
